<template>
  <div class="gl_contentBox gl_heightFull">
    <div class="demo-input-suffix gl_searchBox gl_mb_15">
      <el-row :gutter="20">
        <el-col :span="4">
          <p>地区</p>
          <el-select v-model="provinceCode" placeholder="请选择省份" :disabled="selected[0]" size="mini" filterable @change="changeProvince">
            <el-option v-for="item in provinceList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-select v-model="cityCode" placeholder="请选择城市" :disabled="selected[1]" class="search_btn" size="mini" filterable @change="changeCity">
            <el-option v-for="item in cityList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-select v-model="countryCode" placeholder="请选择区域" :disabled="selected[2]" class="search_btn" size="mini" filterable @change="changeCounty">
            <el-option v-for="item in countryList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <p>姓名/联系方式</p>
          <el-input v-model="qo.Filter" placeholder="请输入姓名/联系方式" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="4">
          <p>证件号码</p>
          <el-input v-model="qo.IdCardNumberFilter" placeholder="请输入证件号码" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="4">
          <p>性别</p>
          <el-select v-model="qo.GenderCode" size="mini" placeholder="请选择性别" @change="getPageList()">
            <el-option label="全部" :value="null" />
            <el-option label="男" value="M" />
            <el-option label="女" value="W" />
          </el-select>
        </el-col>
        <el-col :span="4" class="search_btn" align="left">
          <!-- 查询按钮 -->
          <el-button type="primary" size="mini" @click="getPageList()">筛选</el-button>
          <!-- 重置按钮 -->
          <el-button type="warning" size="mini" @click="clickReset()">{{ $t('button.Reset') }}</el-button>
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="loading" class="gl_table_noBorder" :data="tableData" size="mini">
      <el-table-column label="序号" type="index" />
      <el-table-column label="头像" align="center" width="54">
        <template slot-scope="{row}">
          <PersonalImage :imgurl="row.imagePath" imgheight="60px" />
        </template>
      </el-table-column>
      <el-table-column label="姓名" min-width="100">
        <template slot-scope="{row}">
          {{ row.name }}
        </template>
      </el-table-column>
      <el-table-column label="性别" prop="gender.name" min-width="90" />
      <el-table-column label="证件号码" prop="idCardNumber" min-width="150" />
      <el-table-column label="所属地域" prop="provinceCode" min-width="150">
        <template slot-scope="{row}">
          {{ addressDataForCheck(row.provinceCode, row.cityCode, row.countyCode) }}
        </template>
      </el-table-column>
      <el-table-column label="联系方式" prop="phoneNumber" min-width="100" />
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="clickDetail(row.id)">查看</el-button>
          <el-button size="mini" type="text" @click="clickEdit(row.id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row class="gl_mt_15" style="text-align: right;">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="qo.MaxResultCount"
        :current-page="qo.Page"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-row>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import PersonalImage from '@/components/PersonalImage/index';
import {
  Api_GET_ClubTAManagementRegistersQO // 获取注册人员列表
} from '@/lib/form/hall/swaggerForm';
import {
  api_GET_ClubTAManagementRegisters // 获取注册人员列表
} from '@/extend/api/hallswaggerApi/app';
import {
  api_GET_Provinces,
  api_GET_CitiesProvinceCode,
  api_GET_CountiesCityCode
} from '@/extend/api/gameswaggerApi/district.js';
export default {
  components: { PersonalImage },
  data() {
    return {
      infoCity: {},
      qo: new Api_GET_ClubTAManagementRegistersQO(),
      loading: false,
      tableData: [],
      total: 0,
      provinceCode: null, // 地区c
      cityCode: null,
      countryCode: null,
      provinceList: [],
      cityList: [],
      countryList: [],
      selected: [false, false, false]
    };
  },
  computed: {
    ...mapState({
      cache: state => state.dict.cache,
      dict: state => state.dict.dict,
      currentUserType: state => state.user.currentUserType,
      user: state => state.user.currentOU,
      info: state => state.user.info
    })
  },
  created() {
    this.infoCity = JSON.parse(this.$route.query.infoCity);
    this.getDict();
    this.apiGetProvinces();
    this.init();
    this.getPageList();
  },
  methods: {
    // 若没有字典信息，外部可直接调用方法获取字典数据
    ...mapActions({ getDict: 'dict/getDict' }),
    init() {
      if (this.infoCity) {
        this.selected[0] = Boolean(this.infoCity.provinceCode);
        this.selected[1] = Boolean(this.infoCity.cityCode);
        this.selected[2] = Boolean(this.infoCity.countyCode);
      }
      this.provinceCode = this.infoCity.provinceCode;
      this.cityCode = this.infoCity.cityCode;
      this.countryCode = this.infoCity.countyCode;
      this.provinceCode ? this.apiGetCitiesByProvince() : '';
      this.cityCode ? this.apiGetCountiesByCity() : '';
    },
    getPageList(page) {
      this.qo.Page = page || 1;
      this.qo.SkipCount = (this.qo.Page - 1) * this.qo.MaxResultCount;
      // this.qo.DelegationId = this.currentUserType.delegationId;
      this.qo.ProvinceCode = this.provinceCode;
      this.qo.CityCode = this.cityCode;
      this.qo.CountyCode = this.countryCode;
      this.qo.IncludeGender = true;
      this.qo.IncludeRegisterType = true;
      this.qo.IncludeDelegations = true;
      this.qo.IncludeCTAMemberStatus = true;
      this.qo.IncludeIdCardType = true;
      this.qo.IncludeTags = true;
      this.loading = true;
      api_GET_ClubTAManagementRegisters(this.qo).then(res => {
        this.loading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 查看
    clickDetail(id) {
      this.$router.push({
        name: 'RegisterPersonalInfo',
        query: {
          id: id
        }
      });
    },
    // 编辑
    clickEdit(id) {
      this.$router.push({
        name: 'RegisterPersonalInfo',
        query: {
          id: id,
          edit: true
        }
      });
    },
    // 获取 Provinces 字典
    apiGetProvinces () {
      api_GET_Provinces()
        .then(
          res => {
            res.unshift({ code: null, name: '全部', type: null });
            this.provinceList = res;
          }
        )
        .catch(
          error => {
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          }
        );
    },
    // 获取 Cities 字典
    apiGetCitiesByProvince () {
      api_GET_CitiesProvinceCode(this.provinceCode)
        .then(
          res => {
            res.unshift({ code: null, name: '全部', type: null });
            this.cityList = res;
          }
        )
        .catch(
          error => {
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          }
        );
    },
    // 获取 Counties 字典
    apiGetCountiesByCity () {
      api_GET_CountiesCityCode(this.cityCode)
        .then(
          res => {
            res.unshift({ code: null, name: '全部', type: null });
            this.countryList = res;
          }
        )
        .catch(
          error => {
            const _msg = error.error.message || error.message || '获取失败';
            this.$message({ type: 'error', message: _msg });
          }
        );
    },
    changeProvince(province) {
      this.cityCode = '';
      this.countryCode = '';
      this.cityList = [];
      this.countryList = [];
      if (province !== null) {
        this.cityCode = null;
        this.apiGetCitiesByProvince();
      }
      this.getPageList(this.qo.Page);
    },
    changeCity(city) {
      this.countryCode = '';
      this.countryList = [];
      if (city !== null) {
        this.countryCode = null;
        this.apiGetCountiesByCity();
      }
      this.getPageList(this.qo.Page);
    },
    changeCounty() {
      this.getPageList(this.qo.Page);
    },
    clickReset() {
      this.qo = new Api_GET_ClubTAManagementRegistersQO();
      this.cityList = [];
      this.countryList = [];
      this.init();
      this.provinceCode = this.provinceCode || null;
      this.cityCode = this.cityCode ? this.cityCode : this.provinceCode ? null : '';
      this.countryCode = this.countryCode ? this.countryCode : this.cityCode ? null : '';
      this.getPageList();
    },
    // 处理省市区
    addressDataForCheck (_provinceCode, _cityCode, _countyCode) {
      let _provinceData;
      let _cityData;
      let _countyData;
      if (_provinceCode && this.cache.district) {
        _provinceData = this.cache.district.find(item => item.code === _provinceCode);
      }
      if (_cityCode && _provinceData) {
        _cityData = _provinceData.children.find(item => item.code === _cityCode);
      }
      if (_countyCode && _cityData) {
        _countyData = _cityData.children.find(item => item.code === _countyCode);
      }
      _provinceData = _provinceData ? _provinceData.name : '';
      _cityData = _cityData ? _cityData.name : '';
      _countyData = _countyData ? _countyData.name : '';
      return `${_provinceData} ${_cityData} ${_countyData}`;
    },
    // 分页
    changePage(page) {
      this.getPageList(page);
    },
    changePageSize(pagesize) {
      this.qo.MaxResultCount = pagesize;
      this.getPageList();
    }
  }

};
</script>

<style>

</style>
